var result = new Object();

/**
 * 判断内容是否为空
 * @return {type} 布尔类型：true代表字段为空，false 代表字段不为空
 */
result.isEmpty = function (varr) {
	if(varr==0){
		return false;
	}
	if (varr == null || varr == "") {
		return true;
	}
	return false;
}


/**
 * 生成uuid
 * 默认长度为32位
 * @param {Object} len 长度
 */
result.createUUID = function (len) {
	len = len || 32;
	var $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var maxPos = $chars.length;
	var pwd = '';
	for (var i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}


/**
 * @param {Object} json
 */
result.newJSON = function (json) {
	return JSON.parse(JSON.stringify(json));
}


result.pxToRpx = function (px) {
	return px * 750 / wx.getSystemInfoSync().windowWidth;
};


result.rpxToPx = function (rpx) {
	return rpx / 750 * wx.getSystemInfoSync().windowWidth;
};

/**
 * 获取系统信息
 */
result.getSystemInfo = function () {
	var systemInfo = uni.getSystemInfoSync();
	return systemInfo;
}

/**
 * 获取距离的可读格式
 * @param m
 * @return {{unit: string, num: string}}
 */
result.readableDistance = function (m) {
	var m2 = m + "";
	m2 = m2.substring(m2.indexOf(".") + 1);

	var result = {
		unit: 'm',
		num: m2
	};


	if (m >= 1000) {
		var km = (m / 1000);
		var km2 = km.toFixed(2) + "";
		var aa = km2.substring(km2.indexOf(".") + 1);
		var bb = km2.substring(km2.indexOf(".") + 2);
		if (aa <= 10) {
			km2 = km2.substring(0, km2.indexOf("."));
		}
		var result = {
			unit: 'km',
			num: km2
		};
	}
	return result;
}


/**
 * 获取存储单位的可读格式
 */
result.readableStorage = function (storage) {
	var storage2 = storage + "";
	storage2 = storage2.substring(storage2.indexOf(".") + 1);

	var result = {
		unit: 'B',
		num: storage2
	};
	// 如果大于1kb
	if (storage >= 1024) {
		var kb = (storage / 1024);
		var kb2 = kb.toFixed(2) + "";
		var aa = kb2.substring(kb2.indexOf(".") + 1);
		var bb = kb2.substring(kb2.indexOf(".") + 2);
		if (aa <= 10) {
			kb2 = kb2.substring(0, kb2.indexOf("."));
		}
		return {
			unit: 'KB',
			num: kb2
		};
	}
	// 如果大于1mb
	if (storage >= 1024 * 1024) {
		var mb = (storage / (1024 * 1024));
		var mb2 = mb.toFixed(2) + "";
		var aa = mb2.substring(mb2.indexOf(".") + 1);
		var bb = mb2.substring(mb2.indexOf(".") + 2);
		if (aa <= 10) {
			mb2 = mb2.substring(0, mb2.indexOf("."));
		}
		return {
			unit: 'MB',
			num: mb2
		};
	}

	// 如果大于1gb
	if (storage >= 1024 * 1024 * 1024) {
		var gb = (storage / (1024 * 1024 * 1024));
		var gb2 = gb.toFixed(2) + "";
		var aa = gb2.substring(gb2.indexOf(".") + 1);
		var bb = gb2.substring(gb2.indexOf(".") + 2);
		if (aa <= 10) {
			gb2 = gb2.substring(0, gb2.indexOf("."));
		}
		return {
			unit: 'GB',
			num: gb2
		};
	}

	// 如果大于1tb
	if (storage >= 1024 * 1024 * 1024 * 1024) {
		var tb = (storage / (1024 * 1024 * 1024 * 1024));
		var tb2 = tb.toFixed(2) + "";
		var aa = tb2.substring(tb2.indexOf(".") + 1);
		var bb = tb2.substring(tb2.indexOf(".") + 2);
		if (aa <= 10) {
			tb2 = tb2.substring(0, tb2.indexOf("."));
		}
		return {
			unit: 'TB',
			num: tb2
		};
	}

	return result;

}


/**
 * 给json按照key的字母顺序排序，并返回排序后的json
 */
result.sortJSONByKey = function (json) {
	var arr = new Array();
	for (var key in json) {
		arr.push(key);
	}
	arr.sort();
	var newJson = new Object();
	for (var i = 0; i < arr.length; i++) {
		newJson[arr[i]] = json[arr[i]];
	}
	return newJson;
}


/**
 * 单词首字母大写
 */
result.firstLetterUpper = function (word) {
	return word.substring(0, 1).toUpperCase() + word.substring(1);
}


export default result;
